package com.example.chandao.controller;

import com.example.chandao.common.model.ApiResult;
import com.example.chandao.mapper.ProductMapper;
import com.example.chandao.pojo.Product;
import com.example.chandao.services.ProductService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;
    @Resource
    private ProductMapper productMapper;

    /**
     * 获取产品下的任务数量、需求数量、bug数量以及其状态分布信息
     * @param project
     * @return
     */
    @GetMapping("/count")
    public ApiResult getProject(Product project) {
        LinkedHashMap<Object, Object> resultMap = new LinkedHashMap<>();
        Map<String, List<Product>> collect = productService.getAllProducts(project).stream().collect(Collectors.groupingBy(Product::getProductName));
        for (Map.Entry<String, List<Product>> entry : collect.entrySet()) {
            Map<String, List<Product>> collect1 = entry.getValue().stream().collect(Collectors.groupingBy(Product::getType));
            resultMap.put(entry.getKey(), collect1);
        }
        return ApiResult.success(resultMap);
    }

    /**
     * 获取全部产品下的任务数量、需求数量、bug数量以及其状态分布信息
     * @param project
     * @return
     */
    @GetMapping("/all/count")
    public ApiResult getCount(Product project) {
        Map<String, List<Product>> map = productService.getProductDataCount(project).stream().collect(Collectors.groupingBy(Product::getType));
        return ApiResult.success(map);
    }
}
